package com.briup.jz.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.jz.bean.BaseLog;
import com.briup.jz.bean.BaseLogExample;
import com.briup.jz.dao.BaseLogMapper;
import com.briup.jz.service.IBaseLogService;
import com.briup.jz.utils.CustomerException;

@Service
public class BaseLogServiceImpl implements IBaseLogService {
	
	@Autowired//不加获得不到对象
	private BaseLogMapper baselogmapper;

	@Override
	public void saveOrUpdate(BaseLog baselog) throws CustomerException {
		// TODO Auto-generated method stub
		if(baselog.getId()!=null) {
			baselogmapper.updateByPrimaryKey(baselog);//更新
		} else {
			BaseLogExample example = new BaseLogExample();
			//多条件复合查询
			example.createCriteria().andRealnameEqualTo(baselog.getRealname());//创建新的模板
			List<BaseLog> list = baselogmapper.selectByExample(example);
			if(list.size()>0) {
				throw new CustomerException("同名的分类已经出现");
			}
			baselogmapper.insert(baselog);
		}
		
		
	}

	@Override
	public List<BaseLog> query(String name) {
		// TODO Auto-generated method stub
		//先实例化一个对象
		BaseLogExample example = new BaseLogExample();
		if(name!=null) {
			example.createCriteria().andRealnameLike("%"+name+"%");//模糊查询
		}
		List<BaseLog> list = baselogmapper.selectByExample(example);//??
		return list;
	}

	@Override
	public void deleteById(long id) throws CustomerException {
		// TODO Auto-generated method stub
		BaseLog baselog = baselogmapper.selectByPrimaryKey(id);//??
		if(baselog == null) {
			throw new CustomerException("要删除的信息不存在");//??
		}
		baselogmapper.deleteByPrimaryKey(id);//?
	}
}
